//
//  XTUserAccountListViewModel.swift
//  Weibo(Swift)
//
//  Created by 肖霆 on 16/5/13.
//  Copyright © 2016年 肖霆. All rights reserved.
//

import UIKit

class XTStatusListViewModel: NSObject {

    //保存微博数据
    var statusList:[XTStatusViewModel]?
    
    
    //获取首页微博数据
    func requestStatus(isPullUp:Bool, complete:(isSuccess:Bool,message:String)->()){
        
        var maxId:Int64 = 0
        var sinceId:Int64 = 0
        
        if isPullUp {
            //是上啦加载时
            maxId = statusList?.last?.status?.id ?? 0
            if maxId > 0{
                maxId -= 1
            }
//            print("---\(statusList)")
        }else{
        //下拉刷新时
            sinceId = statusList?.first?.status?.id ?? 0
        }
        
        
       XTStatusDAL.loadData(maxId, sinceId: sinceId) { (statusArray) in
        var message = "没有新的微博"
        if statusArray.count == 0 {
            complete(isSuccess: false,message: message)
        }
                
        //遍历返回的字典数组，组成模型数组
        var tempArray = [XTStatusViewModel]()
        
        for value in statusArray{
            let status = XTStatus(dic: value)
            
            let statusModel = XTStatusViewModel(status: status)
            tempArray.append(statusModel)
        }
        
        
        if self.statusList == nil{
            //进行初始化
            self.statusList = [XTStatusViewModel]()
        }
        
        if isPullUp{
            //上拉加载，将请求得得数据加载原有模型数组的后面
            self.statusList = self.statusList! + tempArray
            
        }else{
            self.statusList = tempArray + self.statusList!
        }
        
        let count = tempArray.count
        if count > 0{
            message = "刷新了\(count)条微博"
        }
        //通过闭包回调数据
        complete(isSuccess: true,message: message)
        
        
        }

        }
    
    
}
